<?php
//dodawanie znaku wodnego o id $watermark_id do $image
function watermark($image_path, $watermark_id, $pos) {
	global $conf;

	//perms
	if(!perms_check('images', 'upload')) {
		plugins('std/unauth/');
		redirect('index.php?module=error&error=auth_error');
		exit();   
	}

	SQLvalidate($watermark_id);

	$db=new dbquery;
	$db->query("SELECT * FROM $conf[prefix]watermark WHERE id=$watermark_id") or $db->err(__FILE__, __LINE__);

	if($db->num_rows() != 0) {
		//image
		$iext = end($foo = explode('.', $image_path));

		switch($iext) {
		case 'jpeg':
		case 'jpg':	$img = imagecreatefromjpeg($image_path);
			break;
		case 'png':	$img = imagecreatefrompng($image_path);
			break;
		case 'gif':	$img = imagecreatefromgif($image_path);
		}

		$imgx = imagesx($img);
		$imgy = imagesy($img);
		//

		//watermark
		$w = $db->fetch_object();
		$wext = end($foo = explode('.', $w->fname));
		$wpath = $conf['watermarks_dir'].$w->fname;

		switch($wext) {
		case 'jpeg':
		case 'jpg':	$wm = imagecreatefromjpeg($wpath);
			break;
		case 'png':	$wm = imagecreatefrompng($wpath);
			break;
		case 'gif':	$wm = imagecreatefromgif($wpath);
		}

		$wmx = imagesx($wm);
		$wmy = imagesy($wm);
		//

		//genenerating image with watermark
		switch($pos) {
		case 1:		$x = 0;
			$y = 0;
			break;
		case 2:		$x = $imgx-$wmx;
			$y = 0;
			break;
		case 3:		$x = 0;
			$y = $imgy-$wmy;
			break;
		case 4:		$x = $imgx-$wmx;
			$y = $imgy-$wmy;
			break;
		default: return 0;
		}

		imagecopy($img, $wm, $x, $y, 0, 0, $wmx, $wmy);

		switch($iext) {
		case 'jpg':
		case 'jpeg': imagejpeg($img, $image_path, 80);
			break;
		case 'png':	imagepng($img, $image_path);
			break;
		case 'gif':	imagegif($img, $image_path);
			break;
		}
		//	
	}
}

?>
